﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Com;
using Core;
using Models;

namespace Controllers
{
    public class XinxiController : AdminBaseController
    {
        xinxiCore core = new xinxiCore();
        public ActionResult GetOne(int id)
        {
            if (!HasQx("article"))
            {
                return Error("没有权限！");
            }
            return Success(core.GetQOne(id));
        }
        public ActionResult GetList(pageinfo pageinfo)
        {
            if (!HasQx("article"))
            {
                return Error("没有权限！");
            }
            List<object> argsList = new List<object>();
            string where = string.Empty;
            //where += " and endtime>=now()";
            if (pageinfo.classid > 0)
            {
                where += " and classid=@" + argsList.Count;
                argsList.Add(pageinfo.classid);
            }
            if (!string.IsNullOrEmpty(pageinfo.key))
            {
                where += " and a.title like @" + argsList.Count + " or a.ID like @"+argsList.Count;
                argsList.Add($"%{pageinfo.key}%");
            }
            if (pageinfo.order == "ordnum")
            {
                pageinfo.order = "a.ordnum";
            }
            var list = core.GetPage(pageinfo.page, pageinfo.pagesize, where, pageinfo.order + " " + pageinfo.esc, argsList.ToArray());
            return Success(list);
        }
        [HttpPost]
        public ActionResult SaveOrdnum(List<xinxiEntity> xinxilist)
        {
            if (!HasQx("article"))
            {
                return Error("没有权限！");
            }
            foreach (var xinxiEntity in xinxilist)
            {
                core.Update(new { xinxiEntity.ordnum }, xinxiEntity.ID);
            }
            return Success();
        }
        [HttpPost]
        public ActionResult SaveFlag(xinxiEntity xinxiEntity)
        {
            if (!HasQx("article"))
            {
                return Error("没有权限！");
            }
            core.Update(new { xinxiEntity.flag }, xinxiEntity.ID);
            return Success();
        }
        [HttpPost]
        public ActionResult Save(qxinxiModel xinxi)
        {
            if (!HasQx("article"))
            {
                return Error("没有权限！");
            }
            try
            {
                if (xinxi.title == null)
                {
                    return Error();
                }

                if (xinxi.ID == 0)
                {
                    xinxi.sid = 1; //TODO 多站点调整
                    xinxi.mid = curmanage.ID;
                    xinxi.addtime = DateTime.Now;
                    xinxi.updatetime = DateTime.Now;
                }

                var model = core.Save(xinxi);
                Log(curmanage.username + "保存文章" + model.ToJson());
                return Success(model);
            }
            catch (Exception e1)
            {
                LoggerManager.Debug(e1.Message, "XinxiController");
                return Error();
            }
        }
        [HttpPost]
        public ActionResult Del(int id)
        {
            if (!HasQx("article"))
            {
                return Error("没有权限！");
            }
            if (id > 0)
            {
                if (core.Del(id))
                {
                    Log(curmanage.username + "删除文章ID=" + id);
                    return Success("删除成功！");
                }

            }
            return Error("删除失败！");
        }
        [HttpPost]
        public ActionResult DelAll(List<xinxiEntity> modelList)
        {
            if (!HasQx("article"))
            {
                return Error("没有权限！");
            }
            string ids = string.Empty;
            foreach (var xinxiEntity in modelList)
            {
                core.Del(xinxiEntity);
                ids += xinxiEntity.ID + ",";
            }
            Log(curmanage.username + "删除文章ID in (" + ids + ")");
            return Success("删除成功！");
        }
    }
}
